<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch">
      <!--Prompt  Identifier-->
      <el-form-item prop="identifier">
        <el-input v-model="queryParams.identifier" placeholder="Pls input prompt name" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <!--Prompt Type-->
      <el-form-item prop="promptType">
        <el-select v-model="queryParams.type" placeholder="Select Type" clearable>
          <el-option v-for="dict in dict.type.prompt_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">Search</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">Reset</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ade:prompt:add']">New</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ade:prompt:export']">Export</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="promptList" @selection-change="handleSelectionChange">
      <el-table-column label="ID" align="center" width="60" prop="id" />
      <el-table-column label="Name" align="left" width="150" prop="identifier" show-overflow-tooltip/>
      <el-table-column label="Type" align="center" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.prompt_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="System Prompt" align="left" prop="systemPrompt" show-overflow-tooltip="" />
      <el-table-column label="User Prompt" align="left" prop="userPrompt" show-overflow-tooltip="" />
      <el-table-column label="Description" align="left" prop="description" show-overflow-tooltip="" />
      <el-table-column label="Actions" width="120" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ade:prompt:edit']">Edit</el-button>
          <el-button v-if="scope.row.flag=='1'" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ade:prompt:remove']">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改Prompt Explore对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" size="mini">
        <el-row :gutter="20">
          <el-col :span="12">
            <!--Prompt 唯一标识-->
            <el-form-item label="Name" prop="identifier" label-width="60px">
              <el-input v-model="form.identifier" placeholder="Pls input identifier" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <!--prompt type-->
            <el-form-item label="Type" prop="type">
              <el-select v-model="form.type" placeholder="Select Type" clearable>
                <el-option v-for="dict in dict.type.prompt_type" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>

          </el-col>
        </el-row>
        <!-- 输入Prompt 描述-->
        <el-form-item label="Description" prop="description">
          <el-input v-model="form.description" type="textarea" :rows="2" placeholder="Pls input description" />
        </el-form-item>

        <!-- 输入 Prompt 模板-->
        <el-row :gutter="20">
          <el-col :span="14">
            <el-form-item label="System Prompt" prop="systemPrompt">
              <el-input v-model="form.systemPrompt" type="textarea" :rows="20"
                placeholder="Pls intput prompt template content.use %s to represent knowledge content." />
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item label="User Prompt" prop="userPrompt">
              <el-input v-model="form.userPrompt" type="textarea" :rows="20"
                placeholder="Pls intput prompt template content. use %s to represent clinical notes/emr content." />
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">Confirm</el-button>
        <el-button @click="cancel">Cancel</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPrompt,
  getPrompt,
  delPrompt,
  addPrompt,
  updatePrompt,
} from "@/api/ade/prompt";

export default {
  name: "Prompt",
  dicts: ['prompt_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // Prompt Explore表格数据
      promptList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        rag: null,
        identifier: null,
        description: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        systemPrompt: [
          { required: true, message: "Prompt can not be empty！", trigger: "blur" },
        ],
        userPrompt: [
          { required: true, message: "Prompt can not be empty！", trigger: "blur" },
        ],
        type: [{ required: true, message: "Type can not be empty!", trigger: "change" }],
        identifier: [
          { required: true, message: "Identifier can not be empty!", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询Prompt Explore列表 */
    getList() {
      this.loading = true;
      listPrompt(this.queryParams).then((response) => {
        this.promptList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userPrompt: null,
        systemPrompt: null,
        rag: null,
        identifier: null,
        flag: null,
        description: null,
        createBy: null,
        createTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "New";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPrompt(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "Edit";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePrompt(this.form).then((response) => {
              this.$modal.msgSuccess("Successfully updated!");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.flag= "1"; // 默认标记为用户级别  0-系统级别
            addPrompt(this.form).then((response) => {
              this.$modal.msgSuccess("Successfully added!");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('Are you suore to delete the Prompt["' + ids + '"]？')
        .then(function () {
          return delPrompt(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("Successfully deleted!");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "ade/prompt/export",
        {
          ...this.queryParams,
        },
        `prompt_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
